package com.iwinding.ladygg.bluetooth.impBle;

import android.os.Handler;
import android.os.Message;

import com.iwinding.ladygg.bluetooth.ibluetooth.IHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 82218 on 2017/7/12.
 */

public class BleHandler extends Handler {

    private List<IHandler> noticeHandlers = null;

    public BleHandler() {
        this.noticeHandlers = new ArrayList<IHandler>();
    }

    @Override
    public void handleMessage(Message msg) {
        if (noticeHandlers.size() != 0) {
            for (IHandler noticeHandler : noticeHandlers) {
                noticeHandler.handleMessage(msg);
            }
        }
        switch (msg.what) {
            case BleManager.SERVICE_BIND:
                BleManager.getInstance().openBle();
                break;
            case BleManager.SERVICE_OPEN:
                BleManager.getInstance().scanBle();
                break;
            case BleManager.SCAN_FINSH:
            case BleManager.CONNET_NEXT:
                BleManager.getInstance().autoConnetBle();
                break;
            case BleManager.CLOSE_BLE:
                BleManager.getInstance().closeBle();
                break;
            case BleManager.CONNECTING:
                break;
            case BleManager.DISCONNECTED:
                break;
            case BleManager.SERVICE_SHOW:
                break;
            case BleManager.PAIRING_REQUEST:
                BleManager.getInstance().setPin("123456");
                break;
            case BleManager.ACTION_FOUND:
//                BleDevice bleDevice = (BleDevice)msg.obj;
                break;
        }
    }

    public void register(IHandler i) {
        if (!noticeHandlers.contains(i))
            noticeHandlers.add(i);
    }

    public void unRegister(IHandler i) {
        if (noticeHandlers.contains(i))
            noticeHandlers.remove(i);
    }

}
